package fr.enseirb.webxml.servlet;
import java.util.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.enseirb.webxml.util.ServletToolkit;

/**
 * Servlet implementation class AboutServlet
 */
public class AboutServlet extends HttpServlet {
	static String teatea = null;
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AboutServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */

	private void setTeatea(String s){
		this.teatea = s;
	}
	private String getTeatea(){
		return this.teatea;
	}
		

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sResponse = "toto" ;
		String studentNumber = "2";
		String lastName;
		String firstName;
		String classe = "informatique 2A";
		String group= "groupe 1";
		Properties params = ServletToolkit.parseURLParams(request);
		String action = params.getProperty("action");
		
		if(request.getRequestURI().contains("about/teacher")){
			sResponse = ServletToolkit.readFile("/resources/html/common/about_teacher.html");

			ServletToolkit.writeResponse(response, sResponse);
			if(request.getRequestURI().contains("about/teacher/post")){
				setTeatea(ServletToolkit.getPostData(request));
				sResponse = getTeatea();
			}
			return;

		}else{
			String studentId = params.getProperty("Id");
			if("1".equals(studentId)){
				if("firstName".equals(action)){ sResponse = "Abid";}else if("lastName".equals(action)){ sResponse = "Rachid";}
			}else if("2".equals(studentId)){
				if("firstName".equals(action)){ sResponse = "Oustlant";}else if("lastName".equals(action)){ sResponse = "Mathilde";}
			}else if("studentNumber".equals(action)) {
				 sResponse = studentNumber;
			}else if("classe".equals(action)){
				sResponse = classe;
			}else if("teacher".equals(action)){
				sResponse = getTeatea();
				
			}else if("group".equals(action)){
				sResponse = group;
			}else{
				sResponse = "il y a " + studentNumber + " personne qui sont Mathilde Oustlant, Rachid Abid, qui sont en " + classe + "  " + group;
			}
		}

		ServletToolkit.writeResponse(response, sResponse);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
